package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.operatesq;
import cn.com.hfuu.module.pick;
import cn.com.hfuu.module.user;
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.module.warehouseteaxlog;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.PickService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.TeaxlogService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.service.ZlService;
import cn.com.hfuu.vo.Page;

@Controller
@RequestMapping("teax")
public class TeaxControl {


	@Autowired
	TeaxService teaxservice;
	
	@Autowired
	TeaxlogService teaxlogservice;
	
	@Autowired
	PickService pickservice;
	
	@Autowired
	OperateService operateservice;
	
	@Autowired
	ZlService zlService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	CodeService codeService;
	
	@RequestMapping("info")
	public String info(Model m){
		return "warehouse/teax/teax";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, warehouseteax teax){
		List<warehouseteax> teaxlist=teaxservice.selectPage(start,limit,teax,1);
		List<warehouseteax> total=teaxservice.selectpagetotal(teax,1);
		for (warehouseteax teax1 : teaxlist) {
			teax1.setPick(pickservice.seletbyid(teax1.getWarehousePickid()));
		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(teaxlist );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
 
 		List<warehouseteax> list = teaxservice.selectpagetotal(new warehouseteax(),1);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (warehouseteax teax1 : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			teax1.setPick(pickservice.seletbyid(teax1.getWarehousePickid()));
			 
			
			map1.put("编号", teax1.getWarehouseTeaxid());
			map1.put("茶名",teax1.getWarehouseTeaname());
			map1.put("重量(克)",""+teax1.getWarehouseTeaquality());
 
			mapList.add(map1);

		}
		String[] title = { "编号", "茶名", "重量(克)" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "鲜叶库存信息");
		return new ModelAndView(ve, map);
	}
	
	@RequestMapping("in")
	public String in(Model m){
		return "warehouse/teax/in";
	}
	
	@RequestMapping("inlist")
	@ResponseBody
	public Page listin(Integer start, Integer limit, warehouseteax teax){
		List<warehouseteax> teaxlist=teaxservice.selectPage(start,limit,teax,0);
		List<warehouseteax> total=teaxservice.selectpagetotal(teax,0);
		for (warehouseteax teax1 : teaxlist) {
			teax1.setPick(pickservice.seletbyid(teax1.getWarehousePickid()));
		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(teaxlist );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("inproc")
	@ResponseBody
	public String inproc(String id, HttpServletRequest req){
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		warehouseteax teax=teaxservice.selectbyid(id);
		SystemUtil sys = new SystemUtil();
		UUIdUtil idu = new UUIdUtil();
		teax.setState(1);
		if(teaxservice.update(teax)>0){
			pick p=pickservice.seletbyid(teax.getWarehousePickid());
			warehouseteaxlog teaxlog = new warehouseteaxlog();
			teaxlog.setWarehouseTeaxlogid(idu.getUuid());
			teaxlog.setWarehouseTeaxid(teax.getWarehouseTeaxid());
			teaxlog.setWarehouseTeaxtype(1);
			teaxlog.setWarehouseUserid(u.getUserId());
			teaxlog.setWarehouseTeaxlogtime(sys.getDatetime());
			teaxlog.setWarehouseTeaxquality(p.getPickQuality());
			teaxlogservice.insert(teaxlog);
			return "success";
		}
		return null;
		
	}
	
	@RequestMapping("out")
	public ModelAndView out(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("warehouse/teax/out");
		List<warehouseteax> teaxlist=teaxservice.selectall();
		mv.addObject("teax", teaxlist);
		return mv;
	}
	
	@RequestMapping("outproc")
	@ResponseBody
	public String outproc(warehouseteax teax,HttpServletRequest req){
		HttpSession s=req.getSession();
		user u=(user) s.getAttribute("user");
		warehouseteax oldt=teaxservice.selectbyid(teax.getWarehouseTeaxid());
		float total=oldt.getWarehouseTeaquality()-teax.getWarehouseTeaquality();
		UUIdUtil idu=new UUIdUtil();
		SystemUtil sys=new SystemUtil();
		//日志
		warehouseteaxlog teaxlog=new warehouseteaxlog();
		teaxlog.setWarehouseTeaxlogid(idu.getUuid());
		teaxlog.setWarehouseTeaxid(teax.getWarehouseTeaxid());
		teaxlog.setWarehouseUserid(u.getUserId());
		teaxlog.setWarehouseTeaxquality(teax.getWarehouseTeaquality());
		teaxlog.setWarehouseTeaxtype(0);
		teaxlog.setWarehouseTeaxlogtime(sys.getDatetime());
		
		
		//加工
		operate o=new operate();
		o.setOperateId(idu.getUuid());
		o.setWarehouseTeaxid(teax.getWarehouseTeaxid());
		o.setWarehouseState(0);
		o.setWarehouseLogid(teaxlog.getWarehouseTeaxlogid());
		if(total<0){
			return "error";
		}
		teax.setWarehouseTeaquality(total);
		if(teaxservice.update(teax)>0){
			//日志处理
			teaxlogservice.insert(teaxlog);
			operateservice.insert(o);
			//加工
			return "success";
		}
		
		return null;
		
	}
	
	
	@RequestMapping("zl")
	public ModelAndView zl(String id){
		ModelAndView mv=new ModelAndView();
		List<code> codelist=codeService.selectbyType("08");
		warehouseteax teax=teaxservice.selectbyid(id);
		if(teax.getWarehouseZlid()!=null||teax.getWarehouseZlid()!=""){
			cn.com.hfuu.module.zl zl=zlService.selectbyid(teax.getWarehouseZlid());
			zl.setUser(userService.selectuserbyid(zl.getZlUserid()));
			for (code code : codelist) {
				if(code.getCodeValue()==zl.getZlResult()){
					zl.setResult(code);
				}System.out.println(zl.toString());
			}
			
			mv.addObject("teax", teax);
			mv.addObject("zl", zl);
		}
		mv.setViewName("print/zlteax");
		return mv;
	}
}
